package controller;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import serviceInterface.UserServiceInterface;
import domain.Person;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	UserServiceInterface userService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(isLogin(request, response)){
			String todo = request.getParameter("todo");
			//Log Out + Home
			if(todo.equals("logout")){
				HttpSession session = request.getSession(true);
				session.setAttribute("logedinUser", null);
				request.setAttribute("message", "Log out successfully!!");
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp");
				dispatcher.forward(request,response);
			}else if(todo.equals("home")){
				HttpSession session = request.getSession(true);
				Person person = (Person) session.getAttribute("logedinUser");
				Person loginPerson = userService.login(person);
				request.setAttribute("loginedRole", loginPerson.getRoles().get(0));
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/mainMenu.jsp");
				dispatcher.forward(request,response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// Log In
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		Person p = new Person();
			p.setUserName(username);
			p.setPassword(password);
		Person loginPerson = userService.login(p);
		if(loginPerson!=null){
			HttpSession session = request.getSession(true);
			session.setAttribute("logedinUser", p);
			
			request.setAttribute("loginedRole", loginPerson.getRoles().get(0));
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/mainMenu.jsp");
			dispatcher.forward(request,response);
		}else{
			request.setAttribute("message", "Wrong username and password!!");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp");
			dispatcher.forward(request,response);
		}
	}

	private boolean isLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		boolean isLogin = false;
		HttpSession session = request.getSession(true);
		Person user = (Person) session.getAttribute("logedinUser");
		if(user!=null){
			isLogin = true; 
		}else{// have log in yet
			isLogin = false;
			System.out.println("User has not loged in yet!!");
			request.setAttribute("message", "Please log in first!!");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp");
			dispatcher.forward(request,response);
		}
		
		return isLogin;
	}
}
